package src

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
	"time"
)

func ToUpLoad(ctx *gin.Context) {

	ctx.HTML(http.StatusOK, "chapter/index.html", nil)
}

func DoUpLoad(ctx *gin.Context) {
	// 获取文件名称
	file, _ := ctx.FormFile("file")
	fmt.Println(file.Filename)

	//时间戳
	tiem_unix_int := time.Now().Unix()
	time_unix_str := strconv.FormatInt(tiem_unix_int, 10)

	dst := "uploadfiles/" + time_unix_str + file.Filename
	ctx.SaveUploadedFile(file, dst)
	ctx.String(http.StatusOK, "上传文件成功！")
}

func ToUpLoad2(ctx *gin.Context) {

	ctx.HTML(http.StatusOK, "chapter/index2.html", nil)
}

func DoUpLoad2(ctx *gin.Context) {

	form, _ := ctx.MultipartForm()
	files := form.File["file"]

	for _, file := range files {
		fmt.Println(file.Filename)
		//时间戳
		tiem_unix_int := time.Now().Unix()
		time_unix_str := strconv.FormatInt(tiem_unix_int, 10)

		dst := "uploadfiles/" + time_unix_str + file.Filename
		ctx.SaveUploadedFile(file, dst)
	}

	ctx.String(http.StatusOK, "上传文件成功！")
}

// ajax 单文件
func ToUpLoad3(ctx *gin.Context) {

	ctx.HTML(http.StatusOK, "chapter/index3.html", nil)
}

func DoUpLoad3(ctx *gin.Context) {
	name := ctx.PostForm("name")
	fmt.Println(name)

	file, _ := ctx.FormFile("file")
	fmt.Println(file.Filename)

	//时间戳
	tiem_unix_int := time.Now().Unix()
	time_unix_str := strconv.FormatInt(tiem_unix_int, 10)

	dst := "uploadfiles/" + time_unix_str + file.Filename
	ctx.SaveUploadedFile(file, dst)
	ctx.String(http.StatusOK, "上传文件成功！")
}

// ajax 多文件
func ToUpLoad4(ctx *gin.Context) {

	ctx.HTML(http.StatusOK, "chapter/index4.html", nil)
}

func DoUpLoad4(ctx *gin.Context) {
	form, _ := ctx.MultipartForm()
	files := form.File["file"]

	name := ctx.PostForm("name")
	fmt.Println(name)

	for _, file := range files {
		fmt.Println(file.Filename)
		tiem_unix_int := time.Now().Unix()
		time_unix_str := strconv.FormatInt(tiem_unix_int, 10)

		dst := "uploadfiles/" + time_unix_str + file.Filename
		ctx.SaveUploadedFile(file, dst)

	}
	//时间戳
	//tiem_unix_int := time.Now().Unix()
	//time_unix_str := strconv.FormatInt(tiem_unix_int, 10)
	//
	//dst := "uploadfiles/" + time_unix_str + file.Filename
	//ctx.SaveUploadedFile(file, dst)
	ctx.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "文件上传成功！",
	})
}
